package com.dingding.order.core.history.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;

import com.dingding.common.core.base.dto.AbstractResponse;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.order.code.order.PublishCarType;
import com.dingding.order.core.history.dao.IHistoryOrderDao;
import com.dingding.order.core.history.dto.TakeMileageDTO;
import com.dingding.order.core.history.service.IHistoryOrderService;
import com.dingding.order.dto.history.HistoryOrderDto;
import com.dingding.order.dto.history.ReqHistoryOrder;
import com.dingding.order.dto.history.ResHistoryOrder;
import com.dingding.order.processer.ProcesserFactory;

/**
 * 
* @ClassName: DriverOrderServiceImpl 
* @Description: 司机订单相关接口
* @author hufan
* @date 2016年11月22日 下午7:07:08 
*
 */
@Component("driverOrderService")
public class DriverOrderServiceImpl extends AbstractResponse<ResHistoryOrder, ReqHistoryOrder> implements IHistoryOrderService{

	@Resource(name = "processerFactory")
	private ProcesserFactory processerFactory;
	
	@Resource(name = "specialHistoryOrderDao")
	private IHistoryOrderDao historyOrderDao;
	
	/**
	 * @Description:获取历史订单列表
	 * @author fengshuonan
	 * @since 2.4
	 */
	@Override
	public String getHistoryOrders(String clientData) {
		
		ResponseEntity<ResHistoryOrder> responseEntity = new ResponseEntity<ResHistoryOrder>();
		ReqHistoryOrder reqHistoryOrder = super.parseRequestJson(clientData);
		
		if(!super.validateParam(responseEntity, reqHistoryOrder)){
			return super.responseBeanToJson(responseEntity);
		}
		
		List<HistoryOrderDto> historyOrderDtos = processerFactory.getOrderProcesser(PublishCarType.SPECIAL)
				.getHistoryOrders(
						reqHistoryOrder.getUserId(), 5, 
						reqHistoryOrder.getPageIndex(), reqHistoryOrder.getPageSize(),reqHistoryOrder.getClientType());
		
		// 获取接乘里程
		TakeMileageDTO takeMileageDTO = historyOrderDao.takeMileage(reqHistoryOrder.getUserId());
		
		ResHistoryOrder resHistoryOrder = new ResHistoryOrder();
		resHistoryOrder.setOrderList(historyOrderDtos);
		
		// 设置接乘里程
		if(takeMileageDTO != null && takeMileageDTO.getPickUpMeters() != null){
			resHistoryOrder.setPickUpMeters(takeMileageDTO.getPickUpMeters());
		}else{
			resHistoryOrder.setPickUpMeters(new BigDecimal(0));
		}
		
		responseEntity.setCode(HttpStatus.SC_OK);
		responseEntity.setPromptMessage("请求成功");
		responseEntity.setResponseBody(resHistoryOrder);
		return super.responseBeanToJson(responseEntity);
	}

	/**
	 * 
	 * @Description:通过车的类型查询历史订单
	 * @author hufan
	 * @since 2.5.6
	 */
	@Override
	public String getHistoryOrdersByCarType(String clientData,final PublishCarType carType) {
		ResponseEntity<ResHistoryOrder> responseEntity = new ResponseEntity<ResHistoryOrder>();
		ReqHistoryOrder reqHistoryOrder = super.parseRequestJson(clientData);
		
		if(!super.validateParam(responseEntity, reqHistoryOrder)){
			return super.responseBeanToJson(responseEntity);
		}
		
		List<HistoryOrderDto> historyOrderDtos = processerFactory.getOrderProcesser(carType)
				.getHistoryOrders(
						reqHistoryOrder.getUserId(), 5, 
						reqHistoryOrder.getPageIndex(), reqHistoryOrder.getPageSize(),reqHistoryOrder.getClientType());
		
		// 获取接乘里程
		TakeMileageDTO takeMileageDTO = null;
		
		if(reqHistoryOrder.getUserRole() == 2){
			takeMileageDTO = historyOrderDao.takeMileage(reqHistoryOrder.getUserId());
		}else if(reqHistoryOrder.getUserRole() == 3){
			takeMileageDTO = historyOrderDao.taxiTakeMileage(reqHistoryOrder.getUserId());
		}
		
		ResHistoryOrder resHistoryOrder = new ResHistoryOrder();
		resHistoryOrder.setOrderList(historyOrderDtos);
		
		// 设置接乘里程
		if(takeMileageDTO != null && takeMileageDTO.getPickUpMeters() != null){
			resHistoryOrder.setPickUpMeters(takeMileageDTO.getPickUpMeters());
			resHistoryOrder.setRideMeters(takeMileageDTO.getPickUpMeters());
		}else{
			resHistoryOrder.setPickUpMeters(new BigDecimal(0));
			resHistoryOrder.setRideMeters(new BigDecimal(0));
		}
		
		responseEntity.setCode(HttpStatus.SC_OK);
		responseEntity.setPromptMessage("请求成功");
		responseEntity.setResponseBody(resHistoryOrder);
		return super.responseBeanToJson(responseEntity);
	}

}
